<?php

namespace app\admin\controller\wadviser;

use app\common\controller\Backend;
use app\common\model\Adviser as AdviserModel;
use app\common\model\Document;
use think\Db;
/**
 * 谋士管理
 *
 * @icon fa fa-user-secret
 */
class Adviser extends Backend
{
    protected $model = null;
    protected $categoryModel = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new AdviserModel;
        $this->categoryModel = model('Wdcate');
        $category = $this->categoryModel->where('status', 'normal')->where('pid',0)->order('weigh', 'desc')->column('name','id');
        $this->view->assign('category', $category);
        $categoryList = $this->categoryModel->where('status', 'normal')->where('pid','>',0)->order('weigh', 'desc')->column('name','id');
        $this->view->assign('categoryList', $categoryList);
        $this->view->assign('userdata', model('user')->where('status','normal')->where('group_id',3)->column('nickname','id'));
    }
    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            $result = array(
                "total" => $list->total(),
                "rows" => $list->items()
            );
            return json($result);
        }
        return $this->view->fetch();
    }

    public function add(){
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            Db::startTrans();
            try {
                $tags = $params['tags'];
                unset($params['tags']);
                $params['after_time'] = strtotime($params['after_time'].' 23:59:59');
                $params['start_time'] = strtotime($params['start_time']);
                $params['end_time'] = strtotime($params['end_time'].' 23:59:59');
                $params['year'] = date('Y', $params['start_time']);
                $params['month'] = date('m', $params['start_time']);
                $doc = AdviserModel::create($params,true);
                $docCate = [];
                foreach ($tags as $value) {
                    $docCate[] = ['adviser_id'=>$doc->id,'wdcate_id'=>$value];
                }
                Db::name('AdviserCate')->insertAll($docCate);
                $document['doc_id'] = $doc->id;
                $document['title'] = $doc->title;
                $document['content'] = $doc->content;
                $document['createtime'] = $doc->createtime;
                $document['updatetime'] = $doc->updatetime;
                $document['is_audit'] = 1;
                $document['type'] = 'adviser';
                Document::create($document,true);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success();
        }
        return $this->view->fetch();
    }

    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            Db::startTrans();
            try {
                $tags = $params['tags'];
                unset($params['tags']);
                $params['after_time'] = strtotime($params['after_time'].' 23:59:59');
                $params['start_time'] = strtotime($params['start_time']);
                $params['end_time'] = strtotime($params['end_time'].' 23:59:59');
                $params['year'] = date('Y', $params['start_time']);
                $params['month'] = date('m', $params['start_time']);
                $row->allowField(true)->save($params);
                $docCate = [];
                foreach ($tags as $value) {
                    $docCate[] = ['adviser_id'=>$row->id,'wdcate_id'=>$value];
                }
                Db::name('AdviserCate')->where('adviser_id', $ids)->delete();
                Db::name('AdviserCate')->insertAll($docCate);
                
                $document['doc_id'] = $row->id;
                $document['title'] = $row->title;
                $document['content'] = $row->content;
                $document['createtime'] = $row->createtime;
                $document['updatetime'] = $row->updatetime;
                
                Db::name('Document')->where('doc_id', $ids)->where('type','adviser')->update($document);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success();
        }
        $row['tags'] = Db::name('AdviserCate')->where('adviser_id', $ids)->column('wdcate_id');

        $this->view->assign('row', $row);
        return $this->view->fetch();
    }
}